package com.canaan.business.service.bill.impl;

import com.canaan.business.domain.bill.PurchaseReturnBillDetail;
import com.canaan.business.domain.vo.PurchaseReturnBillDetailVo;
import com.canaan.business.mapper.bill.PurchaseReturnBillDetailMapper;
import com.canaan.business.service.bill.IPurchaseReturnBillDetailService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 * 采购退货单据明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-13
 */
@Service
public class PurchaseReturnBillDetailServiceImpl implements IPurchaseReturnBillDetailService {
    @Resource
    private PurchaseReturnBillDetailMapper purchaseReturnBillDetailMapper;

    /**
     * 查询采购退货单据明细
     *
     * @param id 采购退货单据明细主键
     * @return 采购退货单据明细
     */
    @Override
    public PurchaseReturnBillDetail selectPurchaseReturnBillDetailById(Long id) {
        return purchaseReturnBillDetailMapper.selectPurchaseReturnBillDetailById(id);
    }

    /**
     * 查询采购退货单据明细列表
     *
     * @param purchaseReturnBillDetail 采购退货单据明细
     * @return 采购退货单据明细
     */
    @Override
    public List<PurchaseReturnBillDetail> selectPurchaseReturnBillDetailList(PurchaseReturnBillDetail purchaseReturnBillDetail) {
        return purchaseReturnBillDetailMapper.selectPurchaseReturnBillDetailList(purchaseReturnBillDetail);
    }

    @Override
    public List<PurchaseReturnBillDetailVo> selectPurchaseReturnBillDetailListPageDatas(PurchaseReturnBillDetail purchaseReturnBillDetail) {
        return purchaseReturnBillDetailMapper.selectPurchaseReturnBillDetailListPageDatas(purchaseReturnBillDetail);
    }

    /**
     * 新增采购退货单据明细
     *
     * @param purchaseReturnBillDetail 采购退货单据明细
     * @return 结果
     */
    @Override
    public int insertPurchaseReturnBillDetail(PurchaseReturnBillDetail purchaseReturnBillDetail) {
        if(StringUtils.isBlank(purchaseReturnBillDetail.getNo())){
            purchaseReturnBillDetail.setNo(TableNoUtil.getNo(PurchaseReturnBillDetail.class));
        }
        if(StringUtils.isEmpty(purchaseReturnBillDetail.getCreateBy())) {
            purchaseReturnBillDetail.setCreateBy(SecurityUtils.getUsername());
        }
        purchaseReturnBillDetail.setCreateTime(DateUtils.getNowDate());
        return purchaseReturnBillDetailMapper.insertPurchaseReturnBillDetail(purchaseReturnBillDetail);
    }

    /**
     * 批量新增采购退货单据明细
     *
     * @param purchaseReturnBillDetailList 采购退货单据明细列表
     * @return 结果
     */
    @Override
    public int batchInsertPurchaseReturnBillDetail(List<PurchaseReturnBillDetail> purchaseReturnBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (PurchaseReturnBillDetail purchaseReturnBillDetail : purchaseReturnBillDetailList){
            if(StringUtils.isBlank(purchaseReturnBillDetail.getNo())){
                purchaseReturnBillDetail.setNo(TableNoUtil.getNo(PurchaseReturnBillDetail.class));
            }
            purchaseReturnBillDetail.setCreateBy(username);
            purchaseReturnBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<purchaseReturnBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > purchaseReturnBillDetailList.size()) {
                endIndex = purchaseReturnBillDetailList.size();
            }
            insertNum = insertNum + purchaseReturnBillDetailMapper.batchInsertPurchaseReturnBillDetail(purchaseReturnBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改采购退货单据明细
     *
     * @param purchaseReturnBillDetail 采购退货单据明细
     * @return 结果
     */
    @Override
    public int updatePurchaseReturnBillDetail(PurchaseReturnBillDetail purchaseReturnBillDetail) {
        purchaseReturnBillDetail.setUpdateBy(SecurityUtils.getUsername());
        purchaseReturnBillDetail.setUpdateTime(DateUtils.getNowDate());
        return purchaseReturnBillDetailMapper.updatePurchaseReturnBillDetail(purchaseReturnBillDetail);
    }

    /**
     * 批量修改采购退货单据明细
     *
     * @param purchaseReturnBillDetailList 采购退货单据明细列表
     * @return 结果
     */
    @Override
    public int batchUpdatePurchaseReturnBillDetail(List<PurchaseReturnBillDetail> purchaseReturnBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (PurchaseReturnBillDetail purchaseReturnBillDetail : purchaseReturnBillDetailList){
            purchaseReturnBillDetail.setUpdateBy(username);
            purchaseReturnBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<purchaseReturnBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > purchaseReturnBillDetailList.size()) {
                endIndex = purchaseReturnBillDetailList.size();
            }
            updateNum = updateNum + purchaseReturnBillDetailMapper.batchUpdatePurchaseReturnBillDetail(purchaseReturnBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除采购退货单据明细
     *
     * @param ids 需要删除的采购退货单据明细主键集合
     * @return 结果
     */
    @Override
    public int deletePurchaseReturnBillDetailByIds(Long[] ids) {
        return purchaseReturnBillDetailMapper.deletePurchaseReturnBillDetailByIds(ids);
    }

    /**
     * 删除采购退货单据明细信息
     *
     * @param id 采购退货单据明细主键
     * @return 结果
     */
    @Override
    public int deletePurchaseReturnBillDetailById(Long id) {
        return purchaseReturnBillDetailMapper.deletePurchaseReturnBillDetailById(id);
    }

    /**
     * 查询采购退货单据明细
     *
     * @param no 编号
     * @return 采购退货单据明细
     */
    @Override
    public PurchaseReturnBillDetail selectPurchaseReturnBillDetailByNo(String no) {
        return purchaseReturnBillDetailMapper.selectPurchaseReturnBillDetailByNo(no);
    }

    /**
     * 批量删除采购退货单据明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deletePurchaseReturnBillDetailByNos(String[] nos) {
        return purchaseReturnBillDetailMapper.deletePurchaseReturnBillDetailByNos(nos);
    }

    /**
     * 删除采购退货单据明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deletePurchaseReturnBillDetailByNo(String no) {
        return purchaseReturnBillDetailMapper.deletePurchaseReturnBillDetailByNo(no);
    }
}
